package com.jerry.hm.datastructure.linkedlist;

import java.util.Iterator;

/**
 * 带哨兵的环形链表
 *
 * @author Jerry
 * @since 2024/9/30 12:28
 */
public class CircleLinkedListSentinel implements Iterable<Integer> {

    private Node sentinel = new Node(null, null, -1);

    private static class Node {

        /**
         * 上一个节点
         */
        Node pre;

        /**
         * 下一个节点
         */
        Node next;

        /**
         * 节点值
         */
        int value;

        public Node(Node pre, Node next, int value) {
            this.pre = pre;
            this.next = next;
            this.value = value;
        }
    }

    public CircleLinkedListSentinel() {
        sentinel.pre = sentinel;
        sentinel.next = sentinel;
    }

    /**
     * 向头部添加一个元素
     *
     * @param value
     */
    public void addFirst(int value) {
        Node target = new Node(sentinel, sentinel.next, value);
        sentinel.next = target;
        sentinel.next.pre = target;
    }

    /**
     * 向尾部添加一个元素
     *
     * @param value
     */
    public void addLast(int value) {
        Node target = new Node(sentinel.pre, sentinel, value);
        sentinel.pre.next = target;
        sentinel.pre = target;
    }

    /**
     * 移除第一个元素
     */
    public void removeFirst() {
        if (sentinel.next == sentinel) {
            throw illegalException(0);
        }
        Node target = sentinel.next;
        sentinel.next = target.next;
        target.next.pre = sentinel;
    }

    /**
     * 删除值为指定值的元素
     *
     * @param value
     */
    public void removeByValue(int value) {
        Node target = findByValue(value);
        if (target == null) {
            return;
        }
        target.pre.next = target.next;
        target.next.pre = target.pre;
    }

    /**
     * 找值未指定值的节点
     *
     * @param value
     * @return
     */
    private Node findByValue(int value) {
        for (Node cur = sentinel.next; cur != sentinel; cur = cur.next) {
            if (cur.value == value) {
                return cur;
            }
        }

        return null;
    }

    /**
     * 移除最后一个元素
     */
    public void removeLast() {
        if (sentinel.next == sentinel) {
            throw illegalException(0);
        }
        Node target = sentinel.pre;
        target.pre.next = sentinel;
        sentinel.pre = target.pre;
    }

    @Override
    public Iterator<Integer> iterator() {

        return new Iterator<>() {

            Node cur = sentinel.next;

            @Override
            public boolean hasNext() {
                return cur != sentinel;
            }

            @Override
            public Integer next() {
                int value = cur.value;
                cur = cur.next;
                return value;
            }
        };
    }

    /**
     * 抛异常
     *
     * @param index
     * @return
     */
    private static RuntimeException illegalException(int index) {
        return new IllegalArgumentException(String.format("index [%d] 不合法%n", index));
    }

}
